
// 这里的几个方法就是照搬了 _friendslist.js中的实现，脑子有点晕，先不复用了
function initOrderSwap2() {
    window.swapByFriendIncome2 = new auixSwap('#by-friend-income2', 'asc', 'desc');
    window.swapByMyIncome2 = new auixSwap('#by-my-income2', 'asc', 'desc');
    window.orderBy2 = 0;
    window.sort2 = 0;
}

function orderByFriendIncome2(el) {
    var curCls = window.swapByFriendIncome2.doSwap();
    tlog(curCls);

    window.orderBy2 = 1; // 'src_price';
    window.sort2 = ('desc' == curCls) ? 0 : 1;  // 和css的class名分离开
    addPageLoading();
    requestAndRenderAddrBooklist(1);

    $api.addCls(el, 'aui-active');
    $api.removeCls($api.byId('by-my-income-frame2'), 'aui-active');
}

function orderByMyIncome2(el) {
    var curCls = window.swapByMyIncome2.doSwap();
    tlog(curCls);

    window.orderBy2 = 2; // 'price';
    window.sort2 = ('desc' == curCls) ? 0 : 1;  // 和css的class名分离开
    addPageLoading();
    requestAndRenderAddrBooklist(1);

    $api.addCls(el, 'aui-active');
    $api.removeCls($api.byId('by-friend-income-frame2'), 'aui-active');
}

function requestAndRenderAddrBooklist(bRefresh) {
    if (bRefresh) {
        window.cur_page = 0;
    }

    var q = {
        query: 'list',
        start: window.cur_page,  // 注意: 是页数，不是起始索引
        count: window.count_per_load,
        orderBy: window.orderBy2,
        sort: window.sort2
    };
	std_ajax_get('wuchen/getaddressbook.php', q, {
        always: function() {
            rmPageLoading();
            rmFooterLoading();
			utils.setRefreshDone();
        },
		succ: function(json, url) {
            if (json && json.list && json.list.length > 0) {
                var html = doT_Render('#tpl-addr-book-list', json.list);
                var dom = $api.byId('addr-book-list');
                if (bRefresh) {
                    $api.html(dom, html);
                } else {
                    $api.append(dom, html);
                }

                sofast();
                window.cur_page++;
            } else {
                if (0 == window.cur_page) {
                    $api.html($api.byId('addr-book-list'), '');
                }

                sofast();
                window.cur_page = -1;
            }
		},
        fail: function(err, url) {
            utils.postErrWithTips(err, url);
        }
	});
}
